# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

from pymongo import MongoClient
from pymysql import Connect
from .settings import *

class MongoPipeline(object):
    def __init__(self,mongo_uri,mongo_db):
        self.mongo_uri = mongo_uri
        self.mongo_db = mongo_db

    @classmethod
    def from_crawler(cls,cralwer):
        return cls(
            mongo_uri=cralwer.settings.get('MONGO_URI'),
            mongo_db=cralwer.settings.get('MONGO_DB')
        )

    def open_spider(self,spider):
        self.client = MongoClient(self.mongo_uri)
        self.db = self.client[self.mongo_db]

    def process_item(self,item,spider):
        self.db[item.collection].update_many({'thirdMatchId':item['thirdMatchId']},{'$set':item},True)
        return item

    def colse_spider(self,spider):
        self.client.close()

class MySqlPipeline(object):

    def open_spider(self,spider):
        self.conn = Connect(host=HOST, port=PORT, user=USER, password=PASSWORD,database=DATABASE,charset='utf8')
        self.cur = self.conn.cursor()

    def process_item(self,item,spider):
        data = dict(item)
        keys = ','.join(data)
        values = ','.join(['%s']*len(data.values()))
        sql = '''INSERT IGNORE INTO {table}({keys})VALUES({values})'''.format(table=item.table,keys=keys,values=values)
        self.cur.execute(sql,tuple(data.values()))
        self.conn.commit()
        return item

    def close_spider(self,spider):
        self.conn.close()

